package top.linyuxb.lycloud.admin.service.impl;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.DigestUtils;
import top.linyuxb.lycloud.admin.entity.LyCloudUserEntity;
import top.linyuxb.lycloud.admin.repository.LyCloudUserRepository;
import top.linyuxb.lycloud.admin.utils.CookieUtil;
import top.linyuxb.lycloud.admin.utils.JacksonUtil;
import top.linyuxb.lycloud.admin.utils.ReturnT;

import java.math.BigInteger;

/**
 * Created by Intellij IDEA.
 *
 * @Author: linyuxb
 * @Date: 2024/7/29 14:43
 * @Desc: 登入服务
 */
@Configuration
public class LoginService {

    public static final String LOGIN_IDENTITY = "LY_CLOUD_LOGIN_IDENTITY";

    @Resource
    private LyCloudUserRepository lyCloudUserRepository;

    private String makeToken(LyCloudUserEntity lyCloudUserEntity) {
        String tokenJson = JacksonUtil.writeValueAsString(lyCloudUserEntity);
        String tokenHex = new BigInteger(tokenJson.getBytes()).toString(16);
        return tokenHex;
    }

    private LyCloudUserEntity parseToken(String tokenHex) {
        LyCloudUserEntity lyCloudUser = null;
        if (tokenHex != null) {
            String tokenJson = new String(new BigInteger(tokenHex, 16).toByteArray());      // username_password(md5)
            lyCloudUser = JacksonUtil.readValue(tokenJson, LyCloudUserEntity.class);
        }
        return lyCloudUser;
    }

    /**
     * login
     *
     * @param response
     * @param usernameParam
     * @param passwordParam
     * @param ifRemember
     * @return
     */
    public ReturnT<String> login(HttpServletResponse response, String usernameParam, String passwordParam, boolean ifRemember) {

        LyCloudUserEntity lyCloudUserEntity = lyCloudUserRepository.findByUsername(usernameParam);
        if (lyCloudUserEntity == null) {
            return new ReturnT<>(500, "账号或密码错误");
        }

        String passwordParamMd5 = DigestUtils.md5DigestAsHex(passwordParam.getBytes());
        if (!lyCloudUserEntity.getPassword().equals(passwordParamMd5)) {
            return new ReturnT<>(500, "账号或密码错误");
        }

        String loginToken = makeToken(lyCloudUserEntity);

        // do login
        CookieUtil.set(response, LOGIN_IDENTITY, loginToken, ifRemember);
        return ReturnT.SUCCESS;
    }

    /**
     * logout
     *
     * @param request
     * @param response
     */
    public void logout(HttpServletRequest request, HttpServletResponse response) {
        CookieUtil.remove(request, response, LOGIN_IDENTITY);
    }

    /**
     * logout
     *
     * @param request
     * @return
     */
    public LyCloudUserEntity ifLogin(HttpServletRequest request) {
        String cookieToken = CookieUtil.getValue(request, LOGIN_IDENTITY);
        if (cookieToken != null) {
            LyCloudUserEntity cookieUser = parseToken(cookieToken);
            if (cookieUser != null) {
                LyCloudUserEntity dbUser = lyCloudUserRepository.findByUsername(cookieUser.getUsername());
                if (dbUser != null) {
                    if (cookieUser.getPassword().equals(dbUser.getPassword())) {
                        return dbUser;
                    }
                }
            }
        }
        return null;
    }

}
